package com.gitee.absolute_spirit.hiot_android.data.source.proto

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.dataStore
import com.gitee.absolute_spirit.hiot_android.NavigateArgs
import kotlinx.coroutines.flow.Flow

class DataStoreManager(val context: Context) {
    private val Context.navigateArgsDataStore: DataStore<NavigateArgs> by dataStore(
        fileName = "data.proto",
        serializer = NavigateArgsSerializer()
    )

    suspend fun saveData(id: Int, type: String) {
        context.navigateArgsDataStore.updateData {args ->
            args.toBuilder().setId(id).setType(type).build()
        }
    }

    val navigateArgsModel: Flow<NavigateArgs> = context.navigateArgsDataStore.data
}